using System;
using System.IO;
using Servlets.Api;
using System.Linq;
using System.Collections.Generic;

namespace Servlets.Http
{
	public class FileUploadBody : IBody
	{
		public FileUploadBody (){}
		
		public object this [string key]{
			get {
				return Value[key];
			}
		}
		
		private IndexableValue<string> values = new IndexableValue<string>();
		private IndexableValue<WebFile> files = new IndexableValue<WebFile>();
		
		public byte[] RawBody{get;private set;}
		public IValue<string> Value {
			get {
				return values;
			}
		}
		public IValue<WebFile> File {
			get {
				return files;
			}
		}
		
		public void ParseStream(StreamReader reader, string contentType, int contentLength) {
//			char[] chars = new char[contentLength];
//			reader.ReadBlock(chars, 0, chars.Length);
//			//byte[] buffer = new byte[contentLength];
//			//reader.BaseStream.Read(buffer, 0, buffer.Length);
//			string wholecontent = new string(chars);
//			Console.WriteLine (wholecontent);
//			byte[] buffer = chars.Select(c => (byte)c).ToArray();
//			MemoryStream memoryStream = new MemoryStream(buffer);
//			RawBody = buffer;
//			
//			string separator = "--"+contentType.Split('=')[1];
//			memoryStream.Flush();
//			memoryStream.Seek(0, SeekOrigin.Begin);
//			ParseStream(memoryStream, separator);
		}
		
/*		private void ParseStream(MemoryStream stream, string separator) {
			BinaryReader binaryReader = new BinaryReader(stream);
			StreamReader textReader = new StreamReader(binaryReader.BaseStream);
			while(true) {
				string s = textReader.ReadLine();
				Console.WriteLine (s);
				Console.WriteLine (separator);
				if(s == "--") {
					break;
				}
				if(s != separator) {
					throw new Exception("Invalid Stream");
				}
			
				string line = textReader.ReadLine();
				string[] split = line.Split(';');
				string key = (split[1].Split('=')[1]).Replace("\"", "");
				
				if(split.Length == 3) {
					int readLength = ReadInFile(textReader, binaryReader, separator, key);
					textReader = new StreamReader(binaryReader.BaseStream);
				} else {
					ReadInValue(textReader, key);
				}
			}
		}
		
		private int ReadInFile(StreamReader textReader, BinaryReader binaryReader, string separator, string key) {
			string contentTypeLine = textReader.ReadLine();
			string blankLine = textReader.ReadLine();
			LinkedList<byte> bytes = new LinkedList<byte>();
			while(true) {
				char c = (char)binaryReader.PeekChar();
				if(c == '-') {
					char[] s = binaryReader.ReadChars(separator.Length);
					string read = new string(s);
					if(read == separator) {
						break;
					} else {
						//still going, rewind
						binaryReader.BaseStream.Seek(-s.Length, SeekOrigin.Current);
					}
				}
				byte b = binaryReader.ReadByte();
				bytes.AddLast(b);
			}
			
			bytes.RemoveLast();
			bytes.RemoveLast();
			bytes.RemoveLast();
			bytes.RemoveLast();
			ContentType contentType = new ContentType(contentTypeLine);
			byte[] content = bytes.ToArray();
			WebFile webfile = new WebFile(contentType, content);
			
			files[key] = webfile;
			int length = content.Length;
			return length;
		}
		
		private void ReadInValue(StreamReader textReader, string key) {
			textReader.ReadLine();
			string val = textReader.ReadLine();
			
			values[key] = val;
		}
		*/
	}
}